<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_musaid extends CI_Model {

    var $musaid_id;
    var $musaid_member_id;
    var $musaid_nama;
    var $musaid_kelamin;
    var $musaid_hp;
    var $musaid_alamat;
    var $musaid_status;
    var $musaid_tanggal;
    var $musaid_oleh;
    var $musaid_dirubah;
    var $cari;
    var $paginate;
    var $num_pages;
    var $offset;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
    }

    public function get_row_all() {
        if ($this->cari) {
            $this->db->where('(musaid_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR musaid_alamat LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        $query = $this->db->count_all_results('t_musaid');

        return $query;
    }

    public function get() {
        if ($this->cari) {
            $this->db->where('(musaid_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR musaid_alamat LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        if ($this->paginate == TRUE) {
            $query = $this->db->get('t_musaid', $this->num_pages, $this->offset);
            return $query;
        }

        $query = $this->db->get('t_musaid');
        return $query;
    }

    public function get_by_id($id = '0') {
        $this->db->where('musaid_id', $id);
        $query = $this->db->get('t_musaid');

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return redirect('homes/');
        }
    }
    
    public function get_musaid_name($name) {
        $this->db->select('musaid_nama');
        $this->db->where('musaid_nama', $name);
        $query = $this->db->get('t_musaid');

        if ($query->num_rows()) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function save() {
        if ($this->musaid_member_id) {
            $db_array['musaid_member_id'] = $this->musaid_member_id;
        }

        if ($this->musaid_nama) {
            $db_array['musaid_nama'] = $this->musaid_nama;
        }

        if ($this->musaid_kelamin) {
            $db_array['musaid_kelamin'] = $this->musaid_kelamin;
        }

        if ($this->musaid_hp) {
            $db_array['musaid_hp'] = $this->musaid_hp;
        }

        if ($this->musaid_alamat) {
            $db_array['musaid_alamat'] = $this->musaid_alamat;
        }

        if ($this->musaid_status) {
            $db_array['musaid_status'] = $this->musaid_status;
        }

        if ($this->musaid_tanggal) {
            $db_array['musaid_tanggal'] = $this->musaid_tanggal;
        }

        if ($this->musaid_oleh) {
            $db_array['musaid_oleh'] = $this->musaid_oleh;
        }

        if ($this->musaid_dirubah) {
            $db_array['musaid_dirubah'] = $this->musaid_dirubah;
        }

        if ($this->musaid_id) {
            $this->db->where('musaid_id', $this->musaid_id);
            $this->db->update('t_musaid', $db_array);
        } else {
            $this->db->insert('t_musaid', $db_array);
        }
    }

    public function delete() {
        $this->db->where('musaid_id', $this->musaid_id);
        $this->db->delete('t_musaid');
    }

    public function set_status() {
        if ($this->musaid_id AND ($this->musaid_status == 0 OR $this->musaid_status == 1)) {
            $oleh = $this->auth->get_member_by_idnya($this->session->userdata('musaid_id'));

            $this->db->where('musaid_id', $this->musaid_id);
            $db_array = array('musaid_status' => $this->musaid_status,
                'musaid_oleh' => $oleh,
                'musaid_dirubah' => date('Y-m-d H:i:s'));
            $this->db->update('t_musaid', $db_array);
        }
    }

}

/* End of file m_musaid.php */
/* Location: ./application/model/m_musaid.php */